package com.difsonapps.android.marker.adapters;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.MainActivity;
import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.activities.LoginActivity;
import com.difsonapps.android.marker.activities.MarksActivity;
import com.difsonapps.android.marker.activities.PhotoViewActivity;
import com.difsonapps.android.marker.app.AppController;
import com.difsonapps.android.marker.beans.Photo;
import com.difsonapps.android.marker.beans.Video;
import com.difsonapps.android.marker.util.MarkerUtil;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

public class PhotoPagerAdapter extends PagerAdapter {
 
    protected Activity _activity;
    private ArrayList<Photo> photos;
    private ArrayList<Video> videos;
    private boolean modePhotos = true;
    //private ArrayList<String> _imagePaths;
    private LayoutInflater inflater;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    MediaController mediaController;
    String avgSym = "";
    String no_mark = "";
    String mark = "";
    String marks = "";
    // constructor
    public PhotoPagerAdapter(Activity activity, ArrayList<Photo> photos) {
        this._activity = activity;
        this.photos = photos;
        init(activity);
    }
    public PhotoPagerAdapter(Activity activity, ArrayList<Video> videos, boolean modePhotos) {
        this._activity = activity;
        this.videos = videos;
        this.modePhotos = modePhotos;
        mediaController = new MediaController(activity);
        init(activity);
    }
    
    private void init(Activity activity){
    	avgSym = activity.getResources().getString(R.string.avg_sym);
        no_mark = activity.getResources().getString(R.string.no_mark);
        mark = activity.getResources().getString(R.string.mark);
        marks = activity.getResources().getString(R.string.marks);
    }
 
    @Override
    public int getCount() {
        return modePhotos?this.photos.size():this.videos.size();
    }
 
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }
     
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    	com.difsonapps.android.marker.VolleyFullImageView imgDisplay;
    	VideoView videoView;
        //Button btnClose;
        TextView image_title;
        TextView image_description;

        TextView nb_marks;
        TextView avg_marks;
  
        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                false);
  
        imgDisplay = (com.difsonapps.android.marker.VolleyFullImageView) viewLayout.findViewById(R.id.imgDisplay);
        ImageView btnClose = (ImageView) viewLayout.findViewById(R.id.btnClose);
        videoView = (VideoView) viewLayout.findViewById(R.id.videoView);
        image_title = (TextView) viewLayout.findViewById(R.id.image_title);
        image_description = (TextView) viewLayout.findViewById(R.id.image_description);
        nb_marks = (TextView) viewLayout.findViewById(R.id.nb_marks);
        avg_marks = (TextView) viewLayout.findViewById(R.id.avg_marks);
        
        imgDisplay.setVisibility(modePhotos?View.VISIBLE:View.GONE);
        videoView.setVisibility(modePhotos?View.GONE:View.VISIBLE);
        
        if(modePhotos){
	        Photo photo = photos.get(position);
	        imgDisplay.setImageUrl(photo.getImage(), imageLoader);
	        image_title.setText(photo.getTitle());
	        image_description.setText(photo.getDescription());
	        nb_marks.setText(""+photo.getNbMarks());
	        avg_marks.setText(""+photo.getAvgMarks());
	        
	        int nbMarks = photo.getNbMarks();
	        double avgMarks = photo.getAvgMarks();
	        nb_marks.setText(""+(nbMarks==0?no_mark:(nbMarks+" "+(nbMarks==1?mark:marks))));
	        avg_marks.setText(avgSym+" "+avgMarks);
	        avg_marks.setVisibility(nbMarks==0?View.GONE:View.VISIBLE);
        }else{
        	Video video = videos.get(position);
        	//videoView.setMediaController(mediaController);
        	videoView.setMediaController(new MediaController(_activity));
        	videoView.setVideoURI(Uri.parse(MarkerUtil.buildImageUrl(video.getUrlVideo(), AppConstant.VIDEOS_FOLDER, null)));
	        //videoView.setVideoPath(video.getUrlVideo());
	        image_title.setText(video.getName());
	        image_description.setText(video.getDescription()+" "+video.getUrlVideo());
	        nb_marks.setText(""+video.getCountMarks());
	        avg_marks.setText(""+video.getAvgMarks());
	        int nbMarks = video.getCountMarks();
	        double avgMarks = video.getAvgMarks();
	        nb_marks.setText(""+(nbMarks==0?no_mark:(nbMarks+" "+(nbMarks==1?mark:marks))));
	        avg_marks.setText(avgSym+" "+avgMarks);
	        avg_marks.setVisibility(nbMarks==0?View.GONE:View.VISIBLE);
        }
        /*BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
        imgDisplay.setImageBitmap(bitmap);*/
         
        // close button click event
        MarkClickListener markClickListener = new MarkClickListener(position);
        nb_marks.setOnClickListener(markClickListener);
        avg_marks.setOnClickListener(markClickListener);
        btnClose.setOnClickListener(new View.OnClickListener() {            
            @Override
            public void onClick(View v) {
                _activity.finish();
            }
        });
  
        ((ViewPager) container).addView(viewLayout);
        //_activity.setTitle(photo.getTitle());
  
        return viewLayout;
    }
     
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);
  
    }
    
    class MarkClickListener implements View.OnClickListener{
    	int position;

		public MarkClickListener(int position) {
			super();
			this.position = position;
		}


		@Override
		public void onClick(View v) {
			showMarks(position);
		}
    	
    }
    
    private void showMarks(int position){
    	//((PhotoViewActivity)_activity).toast("Marks of position "+position);
		Intent intent = new Intent(PhotoPagerAdapter.this._activity, MarksActivity.class);
		Bundle b = new Bundle();
		b.putParcelable("photo", photos.get(position));
		intent.putExtras(b);
		PhotoPagerAdapter.this._activity.startActivity(intent);
    }
}
